package git.soulbgm.utils;

import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @author 贺瑞杰
 * @version V1.0
 * @date 2019-04-17 13:31
 * @description
 */
public class PinyinHelper {

    private final static String REGULAR_EXPRESSION_CHINESE_CHARACTER = "[\u4e00-\u9fa5]+";

    private final static String REGULAR_EXPRESSION_NUMBER = "[0-9]+";

    private final static String REGULAR_EXPRESSION_GRAPHEME = "[a-zA-Z]+";

    /**
     * 将文字转为汉语拼音
     *
     * @param chineseLanguage 要转成拼音的中文
     */
    public static String toPinyin(String chineseLanguage) {
        char[] chars = chineseLanguage.trim().toCharArray();
        StringBuilder pinyin = new StringBuilder();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        // 输出拼音全部小写
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        // 不带声调
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        try {
            for (int i = 0; i < chars.length; i++) {
                if (String.valueOf(chars[i]).matches(REGULAR_EXPRESSION_CHINESE_CHARACTER)) {
                    // 如果字符是中文,则将中文转为汉语拼音
                    pinyin.append(net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(chars[i], defaultFormat)[0]);
                    pinyin.append(" ");
                } else {
                    // 如果字符不是中文,则不转换
                    pinyin.append(chars[i]);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            System.out.println("字符不能转成汉语拼音");
        }
        int length = pinyin.length();
        if (length > 0) {
            pinyin.deleteCharAt(length - 1);
        }
        return pinyin.toString();
    }

    public static String getFirstLettersUp(String chineseLanguage) {
        return getFirstLetters(chineseLanguage, HanyuPinyinCaseType.UPPERCASE);
    }

    public static String getFirstLettersLo(String chineseLanguage) {
        return getFirstLetters(chineseLanguage, HanyuPinyinCaseType.LOWERCASE);
    }

    public static String getFirstLetters(String chineseLanguage, HanyuPinyinCaseType caseType) {
        char[] chars = chineseLanguage.trim().toCharArray();
        String pinyin = "";
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        // 输出拼音全部大写
        defaultFormat.setCaseType(caseType);
        // 不带声调
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        try {
            for (int i = 0; i < chars.length; i++) {
                String str = String.valueOf(chars[i]);
                if (str.matches(REGULAR_EXPRESSION_CHINESE_CHARACTER)) {
                    // 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
                    pinyin += net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(chars[i], defaultFormat)[0].substring(0, 1);
                } else if (str.matches(REGULAR_EXPRESSION_NUMBER)) {
                    // 如果字符是数字,取数字
                    pinyin += chars[i];
                } else if (str.matches(REGULAR_EXPRESSION_GRAPHEME)) {
                    // 如果字符是字母,取字母
                    pinyin += chars[i];
                } else {
                    // 否则不转换
                    //如果是标点符号的话，带着
                    pinyin += chars[i];
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            System.out.println("字符不能转成汉语拼音");
        }
        return pinyin;
    }

    public static String getPinyinString(String chineseLanguage) {
        char[] chars = chineseLanguage.trim().toCharArray();
        String pinyin = "";
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        // 输出拼音全部大写
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        // 不带声调
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        try {
            for (int i = 0; i < chars.length; i++) {
                String str = String.valueOf(chars[i]);
                if (str.matches(REGULAR_EXPRESSION_CHINESE_CHARACTER)) {
                    // 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
                    pinyin += net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(
                            chars[i], defaultFormat)[0];
                } else if (str.matches(REGULAR_EXPRESSION_NUMBER)) {
                    // 如果字符是数字,取数字
                    pinyin += chars[i];
                } else if (str.matches(REGULAR_EXPRESSION_GRAPHEME)) {
                    // 如果字符是字母,取字母
                    pinyin += chars[i];
                } else {
                    // 否则不转换
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            System.out.println("字符不能转成汉语拼音");
        }
        return pinyin;
    }

    /**
     * 取第一个汉字的第一个字符
     *
     * @return String
     * @throws
     * @Title: getFirstLetter
     * @Description: TODO
     */
    public static String getFirstLetter(String chineseLanguage) {
        char[] chars = chineseLanguage.trim().toCharArray();
        String pinyin = "";
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        // 输出拼音全部大写
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        // 不带声调
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        try {
            String str = String.valueOf(chars[0]);
            if (str.matches(REGULAR_EXPRESSION_CHINESE_CHARACTER)) {
                // 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
                pinyin = net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(chars[0], defaultFormat)[0].substring(0, 1);
            } else if (str.matches(REGULAR_EXPRESSION_NUMBER)) {
                // 如果字符是数字,取数字
                pinyin += chars[0];
            } else if (str.matches(REGULAR_EXPRESSION_GRAPHEME)) {
                // 如果字符是字母,取字母
                pinyin += chars[0];
            } else {
                // 否则不转换
                pinyin += "#";
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            System.out.println("字符不能转成汉语拼音");
        }
        return pinyin;
    }

    public static void main(String[] args) {
        System.out.println(PinyinHelper.toPinyin("多发的发独守空房阿道夫打发第三方"));
        System.out.println(PinyinHelper.getFirstLetter("%123H"));
    }

}
